package org.example.controller;

import io.swagger.annotations.ApiOperation;
import org.example.common.CommonConstant;
import org.example.common.ResultData;
import org.example.entity.param.ServiceAddParam;
import org.example.entity.param.ServiceDeleteParam;
import org.example.entity.param.ServiceHeartBeatParam;
import org.example.entity.vo.ServiceVO;
import org.example.service.IRegistryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author sxt
 * @version 1.0
 * @description TODO
 * @date 2023/7/31 21:26
 */
@RestController
@RequestMapping(CommonConstant.API_PREFIX)
public class RegistryController {

    @Resource
    private IRegistryService iRegistryService;

    @PostMapping("/registry")
    @ApiOperation("服务注册")
    private ResultData<Boolean> registry(@RequestBody ServiceAddParam serviceAddParam) {
        boolean registry = iRegistryService.registry(serviceAddParam);
        return registry ? new ResultData<Boolean>().ok(true) : new ResultData<Boolean>().failed(false);
    }

    @DeleteMapping("/unRegistry")
    private ResultData<Boolean> unRegistry(@RequestBody ServiceDeleteParam serviceDeleteParam) {
        boolean registry = iRegistryService.unRegistry(serviceDeleteParam);
        return registry ? new ResultData<Boolean>().ok(true) : new ResultData<Boolean>().failed(false);
    }

    @GetMapping("/heartBeat")
    private ResultData<Boolean> heartBeat(ServiceHeartBeatParam serviceHeartBeatParam) {
        boolean registry = iRegistryService.hearBeat(serviceHeartBeatParam);
        return registry ? new ResultData<Boolean>().ok(true) : new ResultData<Boolean>().failed(false);
    }

    @GetMapping("/discovery")
    private ResultData<List<ServiceVO>> discovery(@RequestParam(name = "name", required = false) String name) {
        ResultData<List<ServiceVO>> ok = new ResultData<List<ServiceVO>>().ok();
        ok.setObj(iRegistryService.discovery(name));
        return ok;
    }

}
